Lecture for IBS519, Fall 2023
Tidyverse
A collection of R packages serving different functions but following the same principles
Designed to work well together
Extremely nerdy
Marvel Universe
A collection of heros/movies with different skills but following the same principles
Frequent crossovers/cameos
Extremely nerdy
One reason that ggplot2 is so popular is that it uses a consistent framework, or “grammar”, which makes it both intuitive AND flexible
Elements of any chart (i.e. axis, points, bars, text etc) are “layered” on top of each other to form the complete graphic
“Good grammar is just the first step in creating a good sentence”
Wickham, H. A Layered Grammar of Graphics. Journal of Computational and Graphical Statistics 19, 3–28 (2010).
Tip
These functions are added to make changes to the default coordinate system or axis scales
They start with the prefixes coord_ or scale_x, scale_y respectively
Tip
Finally, theme() functions can be added to alter the default appearance of the plot
e.g. title, axis titles, font size, background color, etc
You can change attributes of a layer by specifying them as arguments within the geom() function
The aes() function is used to assign variables in your data to graphical parameters (or channels)
Important
Syntax note: Variable names do NOT use quotes when used inside aes()
[1] "data"
A B C D
1 2 3 4 a
2 1 2 1 a
3 4 5 15 b
4 9 10 80 b
Note
Aesthetics set within the original layer will be applied to all later layers/geoms
ggplots can be stored as objects in R
Use ggsave() to save jpg, png, pdf versions of plots
Modifies text labels on plot,axis, etc
labs()
xlab()
ylab()
Change aspects of axis/channel scales
scale_x_continuous()
_discrete()
_manual()
scale_color_continuous()
_discrete()
_manual()
scale_fill_continuous()
_discrete()
_manual()
Add these to ggplots as quick theme changes
theme_grey() default theme in ggplot
theme_bw()
theme_light()
theme_dark()
theme_minimal()
theme_classic()
theme_void()
Extremely customizable
e.g.
ggplot() + theme(axis.text=element_text(size=10))